﻿using System;
using System.IO;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace homework06
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // 获取用户文档目录路径
            string myDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            // 创建根节点并添加到 treeView1
            TreeNode rootNode = treeView1.Nodes.Add("My Documents", "My Documents");
            rootNode.Tag = myDocumentsPath; // 将路径存储在节点的 Tag 属性中

            // 加载用户文档目录下的子目录
            LoadDirectories(myDocumentsPath, rootNode.Nodes);
        }

        private void LoadDirectories(string path, TreeNodeCollection nodes)
        {
            try
            {
                DirectoryInfo directory = new DirectoryInfo(path);
                foreach (DirectoryInfo subDirectory in directory.GetDirectories())
                {
                    TreeNode node = nodes.Add(subDirectory.Name);
                    node.Tag = subDirectory.FullName; // 将完整路径存储在节点的 Tag 属性中
                    LoadDirectories(subDirectory.FullName, node.Nodes);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error loading directory: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            // 获取所选节点的完整路径
            string selectedPath = e.Node.Tag.ToString();
            LoadFiles(selectedPath);
        }
        private void LoadFiles(string path)
        {
            try
            {
                listBox1.Items.Clear();
                DirectoryInfo directory = new DirectoryInfo(path);
                foreach (DirectoryInfo subDirectory in directory.GetDirectories())
                {
                    listBox1.Items.Add(subDirectory.Name);
                }
                foreach (FileInfo file in directory.GetFiles())
                {
                    listBox1.Items.Add(file.Name);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error loading file: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listBox1.SelectedIndex >= 0 && treeView1.SelectedNode != null)
            {
                string selectedPath = treeView1.SelectedNode.Tag.ToString(); // 获取所选节点的完整路径
                string selectedItem = Path.Combine(selectedPath, listBox1.SelectedItem.ToString()); // 使用所选节点的完整路径组合选择的项目路径

                if (File.Exists(selectedItem) && Path.GetExtension(selectedItem).Equals(".txt", StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        System.Diagnostics.Process.Start(selectedItem);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error opening file: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

    }
}
